export const getObjType = function getObjType(obj) {
    var toString = Object.prototype.toString;
    var map = {
        '[object Boolean]': 'boolean',
        '[object Number]': 'number',
        '[object String]': 'string',
        '[object Function]': 'function',
        '[object Array]': 'array',
        '[object Date]': 'date',
        '[object RegExp]': 'regExp',
        '[object Undefined]': 'undefined',
        '[object Null]': 'null',
        '[object Object]': 'object',
    };
    return map[toString.call(obj)];
};

export const getGridOpt = function getGridOpt(opts, width = 300, height = 100) {
    function value(val, len) {
        if ('number' === getObjType(val)) {
            return parseInt(val);
        } else if ('string' === getObjType(val)) {
            return (parseInt(val) * len) / 100;
        }
    }
    let grid = opts.grid || {};
    let top = getObjProperty(grid, 'top', 20),
        left = getObjProperty(grid, 'left', 20),
        right = getObjProperty(grid, 'right', 20),
        bottom = getObjProperty(grid, 'bottom', 20);
    let origin = { x: 20, y: 20, w: 260, h: 160 };

    top = value(top, height);
    left = value(left, width);
    right = value(right, width);
    bottom = value(bottom, height);

    let legendHeight = 30;
    let xAxisHeight = 30;
    origin = {
        x: left,
        y: top + legendHeight,
        w: width - left - right,
        h: height - top - bottom - xAxisHeight - legendHeight,
    };
    return {
        top,
        left,
        right,
        bottom,
        origin,
    };
};

export const getMaxVal = function getMaxVal(arr = []) {
    arr.sort((a, b) => {
        let typea = getObjType(a);
        let typeb = getObjType(b);
        let n1 = a,
            n2 = b;
        if ('string' === typea) {
            n1 = Number(a);
        } else if ('object' === typea) {
            n1 = Number(a.value);
        }
        if ('string' === typeb) {
            n2 = Number(b);
        } else if ('object' === typeb) {
            n2 = Number(b.value);
        }

        if (n2 < n1) {
            return -1;
        }
        if (n2 > n1) {
            return 1;
        }
        return 0;
    });
    if ('object' === getObjType(arr[0])) {
        return arr[0].value;
    }
    return arr[0];
};
export const getMinVal = function getMinVal(arr = []) {
    arr.sort((a, b) => {
        let typea = getObjType(a);
        let typeb = getObjType(b);
        let n1 = a,
            n2 = b;
        if ('string' === typea) {
            n1 = Number(a);
        } else if ('object' === typea) {
            n1 = Number(a.value);
        }
        if ('string' === typeb) {
            n2 = Number(b);
        } else if ('object' === typeb) {
            n2 = Number(b.value);
        }

        if (n1 < n2) {
            return -1;
        }
        if (n1 > n2) {
            return 1;
        }
        return 0;
    });
    if ('object' === getObjType(arr[0])) {
        return arr[0].value;
    }
    return arr[0];
};
export const numFormat = function numFormat(num) {
    let type = getObjType(num);
    let s = num;
    if ('number' === type) {
        s = String(num);
    }
    let s1 = s.split('.')[0] || '';
    let s2 = s.split('.')[1] || '';
    let arr1 = s1.split('');
    let arr2 = s2.split('');

    let len1 = arr1.length;
    let len2 = arr2.length;

    let res1 = [];
    let str1 = s1;
    if (len1 > 3) {
        str1 = '';
        for (let i = 0; i < len1; i++) {
            let ind = i + 1;
            let n = arr1[len1 - ind];
            res1.push(n);
            if (i < len1 && 0 === ind % 3) {
                res1.push(',');
            }
        }
        str1 = res1.reverse().join('');
    }
    let res2 = [];
    let str2 = s2;
    if (len2 > 3) {
        str2 = '';
        for (let i = 0; i < len2; i++) {
            let ind = i + 1;
            let n = arr1[i];
            res2.push(n);
            if (i < len2 && 0 === ind % 3) {
                res2.push(',');
            }
            str2 = res2.join('');
        }
    }
    let res = str1;
    if (str2) {
        res = str1 + '.' + str2;
    }
    return res;
};

export const getAxisData = function getAxisData(max = 0) {
    let base = [
        { val: 100, s: 5 },
        { val: 120, s: 6 },
        { val: 150, s: 5 },
        { val: 160, s: 4 },
        { val: 180, s: 6 },
        { val: 200, s: 5 },
        { val: 240, s: 6 },
        { val: 250, s: 5 },
        { val: 300, s: 6 },
        { val: 320, s: 4 },
        { val: 360, s: 6 },
        { val: 400, s: 4 },
        { val: 450, s: 5 },
        { val: 480, s: 6 },
        { val: 500, s: 5 },
        { val: 600, s: 6 },
        { val: 700, s: 7 },
        { val: 750, s: 5 },
        { val: 800, s: 4 },
        { val: 900, s: 6 },
    ];
    let axis = [
        { val: 5, s: 5 },
        { val: 6, s: 6 },
        { val: 8, s: 4 },
        { val: 10, s: 5 },
        { val: 15, s: 5 },
        { val: 20, s: 4 },
        { val: 25, s: 5 },
        { val: 30, s: 6 },
        { val: 40, s: 4 },
        { val: 50, s: 5 },
        { val: 60, s: 6 },
        { val: 70, s: 7 },
        { val: 80, s: 4 },
        { val: 90, s: 6 },
        ...base,
    ];

    for (let i = 1; i < 19; i++) {
        base.forEach((item) => {
            let xis = {
                val: item.val * Math.pow(10, i),
                s: item.s,
            };
            axis.push(xis);
        });
    }

    let maxItem = axis.find((element) => {
        return element.val >= max;
    });
    let { val, s } = maxItem;
    let res = [0];

    for (let i = 0; i < s; i++) {
        let yx = ((i + 1) * val) / s;
        res.push(yx);
    }
    return res;
};

export const getObjProperty = function getObjProperty(obj, name, def = false) {
    let has = obj.hasOwnProperty(name);
    if (true === has) {
        return obj[name];
    } else {
        return def;
    }
};


export const getFourVal = function getFourVal(val = '') {
    let r = val;
    if ('number' === getObjType(val)) {
        r = String(val);
    }
    let arr = r.split(' ');
    let len = arr.length;
    if (1 === len) {
        return {
            top: val,
            right: val,
            bottom: val,
            left: val,
        };
    } else if (2 === len) {
        return {
            top: arr[0],
            right: arr[1],
            bottom: arr[0],
            left: arr[1],
        };
    } else if (3 === len) {
        return {
            top: arr[0],
            right: arr[1],
            bottom: arr[2],
            left: arr[1],
        };
    } else if (4 === len) {
        return {
            top: arr[0],
            right: arr[1],
            bottom: arr[2],
            left: arr[3],
        };
    }
};

